import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
import tabRoutes from "./router";
import { Toast } from "vant";
import { getSignature } from "@/api/login";
import { getUrl } from "@/utils/auth";
import store from "@/store";
// const originalPush = (VueRouter.prototype.push.push = function push(location) {
//   // 修改 原型对象中的push方法
//   return originalPush.call(this, location).catch((err) => err)
// })
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch(err => err);
};

const routes = tabRoutes;
const router = new VueRouter({
  // base: process.env.BASE_URL,
  // routes,
  mode: "history",
  routes,
  base: "/gbzj/app"
});

// 全局后置钩子
router.afterEach(to => {
  // 设置title
  document.title = to.meta.title;
  window.scrollTo(0, 0);
});

router.beforeEach((to, from, next) => {
  console.log("beforeEach：", to, from);
  const token = to.query.token;
  if (token) {
    localStorage.setItem("client_authorization", token);
    store.commit("SET_TOKEN", token);
    delete to.query.token;
    next(to);
  } else {
    next();
  }
});

export default router;
